#!/bin/bash
#Careful with this script, or you'll wipe data off disks
SCRIPTNAME=mount_volumes
scriptPath=${0%$SCRIPTNAME}
if [ -z "$scriptPath" ]
then
  #invoked from PATH
  scriptPath=$(which $0)
fi
source ${scriptPath}vbs_common_functions
usage()
{
cat << EOF
usage: mount_volumes <options>
OPTIONS:
-f 		Automatically format all non-mounting volumes (Dangerous)
-a		Ask to format non-mounting volumes
-r		Rearrange all volumes(unmounts already mounted ${ROOTDIRS} volumes)
-j		Just mount the volumes, eventhought mounting with mountpoints failed. This is handy if you want to try a different filesystem and mount it fast
EOF
}
PARTED=parted
MKFS=mkfs.ext4
TUNE=tune2fs
FUSE=unionfs-fuse
#Ripped from Mark6
#noatime disabled access time recording
#nodiratime same thing for directories
#writeback rumoured to be fastest option.
#Ext4 could use no barrier as well. But we're disabling journaling so it should be active anyway
MOUNT_OPTS="defaults,data=writeback,noatime,nodiratime"

#Drives can be manually set here like this.
#DRIVES=(sdb sdc sdd sde sdf sdh sdj sdi sdm sdn sdk sdl sdo)
#DRIVES=($(echo sd{a..z} sda{a..j}))

DATUID=$(id -u $USER)
DATGID=$(getent group $GROUP | cut -d: -f3)
FORMAT=0
ASK=0
JUSTMOUNT=0
REARRANGE=0
n=1
while [ $# -gt 0 ]
do
  #Arg1 can come at any time so doing this the boring way
  case $1 in
    -f) 
      FORMAT=1
      ASK=0
      echo "CAUTION: -f set. Formatting set drives if mount shows errors which will erase data on that drive"
      read -n1 -p "Are you sure you want to continue? [y,n]" doit
      case $doit in
	n|N) exit 1;;
      esac
      printf "\n"
      ;;
    -a) 
      FORMAT=1
      ASK=1
      echo "Will ask if format needed"
      ;;
    -r) 
      REARRANGE=1
      ;;
    -j) 
      JUSTMOUNT=1
      ;;
    --help)
      usage;exit 1;
    ;;
    -*) echo "Unknown argument $1";usage;exit 1; echo "Errors in parameters $0" >> $LOGFILE;;
    #File is the extra out
  *) eval "arg_$n=$1";n=$(($n+1));;
  esac
  shift
done

if [ -z "$DRIVES" ]
then
  DONE_WITH_SELECTING=0
  DEVICES=($(ls /dev |egrep 'sd[a-z]{1,2}$'))
  while [ "$DONE_WITH_SELECTING" -eq "0" ]
  do
    j=0
    for i in ${DEVICES[@]}
    do
      echo "${DRIVES[@]}" | grep -o $i &>/dev/null
      if [ "$?" -eq "0" ]
      then
	printf "%2s: [*] %5s\n" $j $i
      else
	printf "%2s: [ ] %5s\n" $j $i
      fi
      j=$(($j+1))
    done
    echo "* = Use this drive"
    echo "Input: c to continue with this setup, <number> to toggle use/don't use, set all on or off"
    read -n2 -p "Action? [[c]ontinue,<number>,[a]ll,e[x]it]: " doit
    case $doit in
      c) break;;
      [0-9]*) 
	if [ "$doit" -ge "${#DEVICES[@]}" ]
	then
	  echo "No such device number $doit"
	  continue
	fi

	if [ ! -z "$(echo ${DRIVES[@]} |grep -o ${DEVICES[$doit]})" ]
	then
	  DRIVES=($(echo ${DRIVES[@]}|sed "s/${DEVICES[$doit]}//g"))
	else
	  DRIVES+=(${DEVICES[$doit]})
	fi
	#echo "toggling ${DEVICES[$doit]}"
	;;
      #[0-9][0-9]) 
	#DRIVES+=(${DEVICES[$doit]})
	##echo ${DEVICES[$doit]}
	#;;
      a)
	if [ -z "$DRIVES" ]
	then
	  DRIVES+=(${DEVICES[@]})
	else
	  DRIVES=( )
	fi
	;;
      x)
	exit -1
	;;
      *)
	echo "Unknown argument: $doit"
	;;
    esac
  done
fi

USED_MOUNTPOINTS=$(mount | grep ${ROOTDIRS}| cut -d ' ' -f 3)
USED_DEVICES=$(mount | grep ${ROOTDIRS}| cut -d ' ' -f 3)

k=0
#What would be cool: Do this next loop in parallel
for i in "${DRIVES[@]}"
do
  if [ "$REARRANGE" -eq "0" ]
  then
    echo $USED_DEVICES | grep -o "$i" &> /dev/null
    if [ "$?" -eq "0" ]
    then
      echo "$i already mounted. Skipping"
      continue
    fi
    echo $USED_MOUNTPOINTS | grep -o "${ROOTDIRS}$k" &>/dev/null
    while [ "$?" -eq "0" ]
    do
      echo "${ROOTDIRS}$k already mounted. Skipping"
      k=$(($k+1))
      echo $USED_MOUNTPOINTS | grep -o "${ROOTDIRS}$k" &>/dev/null
    done
  else
    umount ${ROOTDIRS}$k
  fi
  if [ ! -d "${ROOTDIRS}$k" ]; then
    mkdir ${ROOTDIRS}$k
  fi
  mount -o ${MOUNT_OPTS} /dev/${i}1 ${ROOTDIRS}$k
  if [ $? -ne "0" ]; then
    echo "^ Error from $i"
    if [ "$JUSTMOUNT" -eq "1" ]
    then
      mount /dev/${i}1 ${ROOTDIRS}$k
    elif [ "$FORMAT" -eq "1" ]
    then
      if [ "$ASK" -eq "1" ]
      then
	read -n1 -p "Do you want to reformat $i? [y,n]" doit
	case $doit in
	  n|N) continue;;
	esac
      fi
      ${PARTED} -s /dev/${i} mklabel msdos
      ${PARTED} -s /dev/${i} mkpart primary ext4 0% 100%
      ${MKFS} /dev/${i}1
      #Ripped from Mark6-project disktool
      ${TUNE} -o journal_data_writeback /dev/${i}1
      ${TUNE} -O ^has_journal /dev/${i}1

      mount -o ${MOUNT_OPTS} /dev/${i}1 ${ROOTDIRS}$k
    else
      echo "-f not set. Not formatting. Skipping $i due to error"
    fi
  fi
  chown $USER:$GROUP ${ROOTDIRS}$k/
  chmod ug+rw ${ROOTDIRS}$k/
  k=$((k+1))
done


exit 0
